package com.lxq;

import java.util.ServiceLoader;

/**
 * @author 龙小虬
 * @date 2021/4/12 11:33
 */
public class Main02 {
    static long start;
    static long end;
    public static void main(String[] args) throws ClassNotFoundException {
        MyLoadClass myLoadClass1 = new MyLoadClass("D:\\SpiService03.class");
        Class<?> aClass1 = myLoadClass1.findClass("com.lxq.impl.SpiService03");
        SpiService spiService1 = getSpiService(aClass1);
        System.out.println(spiService1.output());
        start = myLoadClass1.getLastModified();
        new Thread(()->{
            while (true){
                try{
                    Thread.sleep(2000);

                }catch (Exception e){

                }
                end = myLoadClass1.getLastModified();
                if(end!=start) {
                    MyLoadClass myLoadClass2 = new MyLoadClass("D:\\SpiService03.class");
                    try {
                        Class<?> aClass2 = myLoadClass2.findClass("com.lxq.impl.SpiService03");
                        SpiService spiService2 = getSpiService(aClass2);
                        System.out.println(spiService2.output());
                        start = end;
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    static SpiService getSpiService(Class<?> aClass){
        try {
            return (SpiService)aClass.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}
